$(document).ready(function () {



    GetOwnedTeams();





    $('.TeamDeleteIcon').live('click', function () {
        var id = $(this).parent().find('.draggableHidden').val();
        DeleteMemberFromTeam(id, undefined);
    });



    $('#CreateTeam').click(function () {

        var newTeamName = $('#TeamName').val();
        if (newTeamName == undefined || newTeamName == '') {
            $('#TeamName').addClass('Error');
        }
        else {
            $('#TeamName').removeClass('Error');
            $.post("/Team/SaveNewTeam", { teamName: newTeamName },
            function (data) {
                if (data.Success != undefined && data.Success) {
                    showOkDialog('Sucessfully added team [' + newTeamName + "]", 180, 'Sucess');
                    GetOwnedTeams();
                }
                else {
                    showOkDialog(data.Message, 180, 'Error');
                }
            },
            "json");
        }
    });






    $(".droppable").droppable({
        hoverClass: "ui-state-active",
        drop: function (event, ui) {
            $(this).addClass("ui-state-highlight");

            var fullDragUI = $(ui.draggable.context);
            var id = fullDragUI.find('.draggableHidden').val();
            DeleteMemberFromTeam(id, ui.draggable);
        }
    });


    $('#AssignUser').click(function () {
        $.post("/Team/AssignMemberToTeam", { teamId: $('#OwnedTeams').val(), teamMemberId: $('#FoundUsers').val() },
        function (data) {
            if (data.Success != undefined && data.Success) {
                GetTeamMembersForSpecificTeam($('#OwnedTeams').val());
            }
            else {
                showOkDialog(data.Message, 180, 'Error');
            }
        },
        "json");
    });



    $('#OwnedTeams').change(function () {
        GetTeamMembersForSpecificTeam($('#OwnedTeams').val());
    });



    $('#SearchForUsers').click(function () {

        var emailToSearchFor = $('#Email').val();

        if (emailToSearchFor == undefined || emailToSearchFor == '') {
            $('#Email').addClass('Error');
        }
        else {
            $.post("/Team/SearchForUsers", { email: $('#Email').val(), isOpenIdlogin: $('#IsOpenIdLogin').is(':checked') },
                function (data) {
                    if (data.Success != undefined && data.Success) {

                        if (data.Message.length > 0) {
                            $('#FoundUsersPanel').show();

                            populateSelect('#FoundUsers', data.Message,
                                function (input) { return input.UserId; },
                                function (input) { return input.UserName; });

                            $("#FoundUsers").show();

                        }
                        else {
                            $('#FoundUsersPanel').hide();
                        }

                    }
                    else {
                        showOkDialog(data.Message, 180, 'Error');
                    }
                },
                "json");
        }
    });
});



function DeleteMemberFromTeam(userId, draggable) {

    showYesNoDialog('Are you sure you want to delete this user from the selected team?', 180, 'Confirm',
                function () {
                    $.post("/Team/DeleteMemberFromTeam", { teamId: $('#OwnedTeams').val(), teamMemberId: userId },
                        function (data) {
                            if (data.Success != undefined && data.Success) {
                                GetTeamMembersForSpecificTeam($('#OwnedTeams').val());
                            }
                            else {
                                //animate draggable back currentDraggablePosition
                                AnimateDraggableBack(draggable);
                                showOkDialog(data.Message, 180, 'Error');
                            }
                        },
                        "json");
                },
                function () {
                    //animate draggable back currentDraggablePosition
                    AnimateDraggableBack(draggable);

                }
            );
}







function GetTeamMembersForSpecificTeam(teamId) {

    $("#teamMemberContainer").empty();

    $.post("/Team/GetTeamMembersForSpecificTeam", { teamId: teamId },
            function (data) {
                if (data.Success != undefined && data.Success) {
                    if (data.Message.length > 0) {
                        $("#teamMemberTemplate").tmpl(data.Message).appendTo("#teamMemberContainer");
                        $("#TeamMembersPanel").show();
                    }
                    else {
                        $("#TeamMembersPanel").hide();
                    }
                }
                else {
                    $("#TeamMembersPanel").hide();
                    showOkDialog(data.Message, 180, 'Error');
                }


                $(".draggable").tooltip({ effect: 'slide' });

                $(".draggable").draggable({

                    revert: 'invalid',
                    stop: function () {
                        $(this).draggable('option', 'revert', 'invalid');
                    },
                    start: function (event, ui) {
                        $('#CurrentDraggablePosition').data(ui.position);
                    },
                    drag: function (event, ui) {
                        $(".draggable").each(function () {
                            $(this).tooltip().hide();
                        });
                    }
                });

            },
            "json");
}




function GetOwnedTeams() {

    $.post("/Team/GetOwnedTeams",
            function (data) {
                if (data.Success != undefined && data.Success) {
                    if (data.Message.length > 0) {
                        $('#OwnedTeams').show();
                        $('#AssignToTeamPanel').show();

                        populateSelect('#OwnedTeams', data.Message,
                            function (input) { return input.TeamId; },
                            function (input) { return input.TeamDescription; });

                        $("#OwnedTeams").show();
                        GetTeamMembersForSpecificTeam($('#OwnedTeams').val());
                    }
                    else {
                        $('#OwnedTeams').hide();
                        $('#AssignToTeamPanel').hide();
                    }
                }
                else {
                    $('#OwnedTeams').hide();
                    $('#AssignToTeamPanel').hide();
                }
            },
            "json");
}



function AnimateDraggableBack(element) {

    if (element != undefined) {
        //animate draggable back currentDraggablePosition
        element.animate({
            left: $('#CurrentDraggablePosition').data().left,
            top: $('#CurrentDraggablePosition').data().top
        }, 600, "easeOutElastic");
    }

}


